package com.briup.emdc.server.impl;

import com.briup.emdc.bean.Env;
import com.briup.emdc.config.Configuration;
import com.briup.emdc.config.Log;
import com.briup.emdc.server.DBStore;
import com.briup.emdc.utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Properties;

/**
 * @author Kevin
 * @since 2024.04.10
 **/
public class DBStoreImpl implements DBStore{
    private static final String CONCACT_CHAR="_";
    private Log log;
    private String dbName;
    private String tablePrefix;
    private String zoneId;
    private final Connection conn;
    private final Statement st;

    public DBStoreImpl() throws Exception{
        this.conn=DBUtils.getConn();
        this.st=this.conn.createStatement();
    }

    @Override
    public void saveDb(final Collection<Env> coll) throws Exception{
        // 创建数据库：emdc
        String createDBSQL="create database if not exists "+dbName;
        this.st.execute(createDBSQL);

        for(Env env: coll){
            Timestamp ts=env.getGatherDate();
            Instant ins=Instant.ofEpochMilli(ts.getTime());
            LocalDateTime dt=LocalDateTime.ofInstant(ins,ZoneId.of(zoneId));
            int year=dt.getYear();
            int month=dt.getMonth().getValue();
            int day=dt.getDayOfMonth();
            String tableName=String.join(CONCACT_CHAR,tablePrefix,""+year,""+month,""+day);
            String crtTableSql="create table if not exists emdc."+tableName+"("+"name varchar(20),"+"src_id varchar(20),"+"dst_id varchar(20),"+"dev_id varchar(20),"+"sersor_address varchar(20),"+"count int,"+"cmd varchar(20),"+"status int,"+"data float(10,2),"+"gather_date timestamp)";
            boolean failed=this.st.execute(crtTableSql);
            if(!failed){
                String sql="insert into emdc."+tableName+" values(?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement ps=this.conn.prepareStatement(sql);
                ps.setString(1,env.getName());
                ps.setString(2,env.getSrcId());
                ps.setString(3,env.getDstId());
                ps.setString(4,env.getDevId());
                ps.setString(5,env.getSersorAddress());
                ps.setInt(6,env.getCount());
                ps.setString(7,env.getCmd());
                ps.setInt(8,env.getStatus());
                ps.setFloat(9,env.getData());
                ps.setTimestamp(10,env.getGatherDate());
                ps.executeUpdate();
                log.info("添加了一条数据！");
            }
        }
    }

    @Override
    public void init(final Properties prop) throws Exception{
        dbName=prop.getProperty("dbname");
        tablePrefix=prop.getProperty("table-prefix");
        zoneId=prop.getProperty("zoneid");
    }

    @Override
    public void configure(final Configuration conf) throws Exception{
        this.log = conf.getLogger();
    }
}
